
# 交叉编译执行 cmake ../ -DCMAKE_TOOLCHAIN_FILE=../rv32.cmake

project(ag32rv2k C ASM)

set(USE_USB ON)

# 主要搜索源码
#    如果添加源码.应该添加到user中
file(GLOB_RECURSE sources CONFIGURE_DEPENDS
    src/*.c
    user/*.c
    misc/*.s
    misc/*.c
    yumi/*.c
)

if(USE_USB)
    file(GLOB_RECURSE usb_src CONFIGURE_DEPENDS
        lib/framework-agrv_tinyusb/src/*.c
        lib/framework-agrv_tinyusb/hw/mcu/agm/*.c
        project/usb/src/*.c
    )
    include_directories(
        lib/framework-agrv_tinyusb/src/
        project/usb/src/
    )

    add_compile_definitions(
        CFG_TUSB_MCU=8800
    )

endif()

message(STATUS "${sources}.")

#添加头文件搜索路径  -I
include_directories(
    misc/
    src/
    ../mcu_flash
)

# 添加宏定义 -D xxx
add_compile_definitions(
    BOARD_HSE_FREQUENCY=12000000
    C_USE_QSPI=0
    C_USE_OPI=0
    C_USE_AHB_ROM=1
)

file(GLOB_RECURSE gdb_script CONFIGURE_DEPENDS
    gdb/load_bin
)

message(STATUS "${gdb_script}.")

add_executable(
    main.elf ${sources} ${usb_src}
)

# 目标生成命令后
# 输出目标数据.  objcopy
add_custom_command(
    TARGET main.elf POST_BUILD
    COMMAND ${CMAKE_OBJCOPY} -O binary main.elf main.bin
    COMMAND ${CMAKE_OBJCOPY} -O ihex main.elf main.hex
)

# 定义目标.已经依赖

add_custom_target(
    load_sram_pre
    lua ${CMAKE_CURRENT_LIST_DIR}/tools/gen_gdb_load.lua ${gdb_script} ${CMAKE_CURRENT_BINARY_DIR}/main.bin
    DEPENDS main.elf
)

add_custom_target(
    load_sram
    start ${CMAKE_GDB} -x ${gdb_script} ${CMAKE_CURRENT_BINARY_DIR}/main.elf
    DEPENDS load_sram_pre
)
